#include <TimerOne.h>
#include <HttpPacket.h>
#include <ArduinoJson.h>
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 8 //DHT11 连接ARDUINO 8


HttpPacketHead packet;


#define DebugSerial Serial
#define ESP8266Serail Serial3

#define Success 1U
#define Failure 0U

int L = 13; //LED指示灯引脚

unsigned long  Time_Cont = 0;       //定时器计数器

const unsigned int esp8266RxBufferLength = 600;
char esp8266RxBuffer[esp8266RxBufferLength];
unsigned int ii = 0;
char OneNetServer[] = "api.heclouds.com";     //不需要修改

const char ssid[] = "610-1";     //修改为自己的路由器用户名
const char password[] = "0123456789"; //修改为自己的路由器密码



char device_id[] = "5349711";    //修改为自己的设备ID
char API_KEY[] = "OmqhyDjWvBzwNYd8B9Kn7STb38U=";    //修改为自己的API_KEY
char sensor_id1[] = "TEMP";
char sensor_id2[] = "HUMI";



void setup() {
    pinMode(L, OUTPUT);
    digitalWrite(L, LOW);
    DebugSerial.begin(9600);
    ESP8266Serail.begin(115200);

    Timer1.initialize(1000);
    Timer1.attachInterrupt(Timer1_handler);

    initEsp8266();


    DebugSerial.println("setup end!");
}

void loop() {
    //获取温湿度数据
    int chk = 25;                             //读取温湿度值

      //串口调试DHT11输出信息
      DebugSerial.print("Read sensor: ");
      switch (chk)
      {
      case DHTLIB_OK:
        DebugSerial.println("OK");
        break;
      case DHTLIB_ERROR_CHECKSUM:
        DebugSerial.println("Checksum error");
        break;
      case DHTLIB_ERROR_TIMEOUT:
        DebugSerial.println("Time out error");
        break;
      default:
        DebugSerial.println("Unknown error");
        break;
      }

      //发送数据到Onenet
      postDataToOneNet(API_KEY,device_id,sensor_id1,random(100));     
      postDataToOneNet(API_KEY,device_id,sensor_id2,random(100));

      delay(5000);
}

void postDataToOneNet(char* API_VALUE_temp,char* device_id_temp,char* sensor_id_temp,double thisData)
{
        //合成POST请求
    StaticJsonBuffer<200> jsonBuffer;



    JsonObject& value = jsonBuffer.createObject();
    value["value"] = thisData;

    JsonObject& id_datapoints = jsonBuffer.createObject();
    id_datapoints["id"] = sensor_id_temp;
    JsonArray& datapoints = id_datapoints.createNestedArray("datapoints");
    datapoints.add(value);

    JsonObject& myJson = jsonBuffer.createObject();
    JsonArray& datastreams = myJson.createNestedArray("datastreams");
    datastreams.add(id_datapoints);

    char p[200];
    int num = myJson.printTo(p, sizeof(p));


    packet.setHostAddress(OneNetServer);
    packet.setDevId(device_id_temp);   //device_id
    packet.setAccessKey(API_VALUE_temp);  //API_KEY
    // packet.setDataStreamId("<datastream_id>");    //datastream_id
    // packet.setTriggerId("<trigger_id>");
    // packet.setBinIdx("<bin_index>");

    /*create the http message about add datapoint */
    packet.createCmdPacket(POST, TYPE_DATAPOINT, p);
    // if (strlen(packet.content))
    //  Serial.print(packet.content);
    // Serial.print(p);
    int httpLength = strlen(packet.content) + num;

    

        //连接服务器
    char cmd[400];
    memset(cmd, 0, 400);    //清空cmd
    strcpy(cmd, "AT+CIPSTART=\"TCP\",\"");
    strcat(cmd, OneNetServer);
    strcat(cmd, "\",80\r\n");
    if (sendCommand(cmd, "CONNECT", 7, 10000, 5) == Success);
    else ESP8266_ERROR(1);

    //发送数据
    memset(cmd, 0, 400);    //清空cmd
    sprintf(cmd, "AT+CIPSEND=%d\r\n", httpLength);
    if (sendCommand(cmd, ">", 1, 3000, 1) == Success);
    else ESP8266_ERROR(2);

    memset(cmd, 0, 400);    //清空cmd
    strcpy(cmd, packet.content);
    strcat(cmd, p);
    if (sendCommand(cmd, "\"succ\"}", 7, 3000, 1) == Success);
    else ESP8266_ERROR(3);

    if (sendCommand("AT+CIPCLOSE\r\n", "CLOSED", 6, 3000, 1) == Success);
    else ESP8266_ERROR(4);
}

void initEsp8266()
{
    if (sendCommand("AT\r\n", "OK", 2, 3000, 10) == Success);
    else ESP8266_ERROR(5);

    if (sendCommand("AT+RST\r\n", "ready", 5, 10000, 10) == Success);
    else ESP8266_ERROR(6);

    if (sendCommand("AT+CWMODE=1\r\n", "OK", 2, 3000, 10) == Success);
    else ESP8266_ERROR(7);

    char cmd[50];
    strcpy(cmd, "AT+CWJAP=\"");
    strcat(cmd, ssid);
    strcat(cmd, "\",\"");
    strcat(cmd, password);
    strcat(cmd, "\"\r\n");

    if (sendCommand(cmd, "OK", 2, 20000, 10) == Success);
    else ESP8266_ERROR(8);

    if (sendCommand("AT+CIPMUX=0\r\n", "OK", 2, 3000, 10) == Success);
    else ESP8266_ERROR(9);

    if (sendCommand("AT+CIFSR\r\n", "OK", 2, 20000, 10) == Success);
    else ESP8266_ERROR(10);
}

void ESP8266_ERROR(int num)
{
    DebugSerial.print("ERROR");
    DebugSerial.println(num);
    while (1)
    {
        digitalWrite(L, HIGH);
        delay(300);
        digitalWrite(L, LOW);
        delay(300);
    }
}



unsigned int sendCommand(char *Command, char *Response, unsigned int Res_Length, unsigned long Timeout, unsigned char Retry)
{
    clrEsp8266RxBuffer();
    for (unsigned char n = 0; n < Retry; n++)
    {
        DebugSerial.print("\r\nsend AT Command:\r\n----------\r\n");
        DebugSerial.write(Command);

        ESP8266Serail.write(Command);

        Time_Cont = 0;
        while (Time_Cont < Timeout)
        {
            esp8266ReadBuffer();
            if ((mystrstr(esp8266RxBuffer, Response, ii, Res_Length)) != NULL)
            {
                DebugSerial.print("\r\nreceive AT Command:\r\n==========\r\n");
                DebugSerial.print(esp8266RxBuffer); //输出接收到的信息
                clrEsp8266RxBuffer();
                return Success;
            }
        }
        Time_Cont = 0;
    }
    DebugSerial.print("\r\nreceive AT Command:\r\n==========\r\n");
    DebugSerial.print(esp8266RxBuffer);//输出接收到的信息
    clrEsp8266RxBuffer();
    return Failure;
}

unsigned char mystrstr(char *s, char *t, unsigned int Length_s, unsigned int Length_t)
{   char x = 0; char *p; p = t;
    int i = 0, j = 0;
    for (; i < Length_s; s++, i++)
    {
        while (*t == *s)
        {   s++; t++; i++; j++;
            if (j >= Length_t) return 1;
        }
        s -= j;
        t = p; j = 0;
    }
    return NULL;
}


void Timer1_handler(void)
{
    Time_Cont++;
}



void esp8266ReadBuffer() {
    while (ESP8266Serail.available())
    {
        esp8266RxBuffer[ii++] = ESP8266Serail.read();
        if (ii == esp8266RxBufferLength)clrEsp8266RxBuffer();
    }
}

void clrEsp8266RxBuffer(void)
{
    memset(esp8266RxBuffer, 0, esp8266RxBufferLength);      //清空
    ii = 0;
}

